import { reqRecommendInfo, reqCateList, reqSearchInfo } from "@/api";

const actions = {
  //用于获取首页推荐信息
  async getRecommendInfo({ commit }) {
    const result = await reqRecommendInfo();
    // console.log(result);
    //判读业务逻辑是否成功
    if (result.code === "200") {
      let arr = [];
      result.data.data.categoryHotSellModule.categoryList.forEach((item) => {
        let obj = {
          categoryName: item.extra.operationResource.categoryName,
          categoryId: item.extra.operationResource.categoryId,
        };
        arr.push(obj);
      });
      // console.log(arr)
      commit("SAVERECOMMENDINFO", { data: result.data.data, arr });
      // console.log(result.data.data)
    } else {
      //业务逻辑失败
      alert(result.message);
    }
  },
  //用于获取家居列表
  async getCateList({ commit }, { categoryId, timestamp }) {
    const result = await reqCateList(categoryId, timestamp);
    // console.log(result.data)
    commit("SAVECATELIST", result.data);
  },
  // 用户获取关键字和热门搜索
  async getSearchInfo({ commit }) {
    const result = await reqSearchInfo();
    // console.log(result.data);
    if (result.code === "200") {
      commit("SAVESEARCHINFO", result.data);
    } else {
      // 业务逻辑失败
      alert(result.message);
    }
  },
};

const mutations = {
  SAVERECOMMENDINFO(state, { data, arr }) {
    state.recommendInfo = data;
    state.arr = arr;
  },
  SAVECATELIST(state, data) {
    state.cateList = data;
  },
  //获取关键字和热门搜索
  SAVESEARCHINFO(state, data) {
    state.searchInfo = data;
  },
};
const state = {
  recommendInfo: {
    kingKongModule: {},
    categoryHotSellModule: {
      categoryList: [],
    },
  }, //首页推荐页面的数据
  arr: [],

  //
  cateList: {
    categoryItemList: [],
    currentCategory: {
      bannerList: [],
    },
  },
  // 存储关键字和热门搜索数据
  searchInfo: {},
};

const getters = {};

export default {
  actions,
  mutations,
  state,
  getters,
};
